package com.emep.changzhi.analyse.utils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/9/5 15:36
 */
public class MapToObject<T> {
    public T convert(T t, Map map) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                String propertyType = descriptor.getPropertyType().getName();

                if (map.containsKey(propertyName)) {
                    Object value = map.get(propertyName);
                    Object[] args = new Object[1];
                    if (propertyType.indexOf("Double") != -1) {
                        args[0] = Double.parseDouble(value.toString());
                    } else if (propertyType.indexOf("Integer") != -1) {
                        args[0] = Integer.parseInt(value.toString());
                    } else if (propertyType.indexOf("Long") != -1) {
                        args[0] = Long.parseLong(value.toString());
                    } else {
                        args[0] = value;
                    }
                    descriptor.getWriteMethod().invoke(t, args);
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return t;
    }

    public boolean convertEntity(T t, Map map) {
        boolean b=false;
        if (map.size()==0){
            return b;
        }
            try {
                BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
                PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
                for (int i = 0; i < propertyDescriptors.length; i++) {
                    PropertyDescriptor descriptor = propertyDescriptors[i];
                    String propertyName = descriptor.getName();
                    String propertyType = descriptor.getPropertyType().getName();

                    if (map.containsKey(propertyName)) {
                        Object value = map.get(propertyName);
                        Object[] args = new Object[1];
                        if (propertyType.indexOf("Double") != -1) {
                            args[0] = Double.parseDouble(value.toString());
                        } else if (propertyType.indexOf("Integer") != -1) {
                            args[0] = Integer.parseInt(value.toString());
                        } else if (propertyType.indexOf("Long") != -1) {
                            args[0] = Long.parseLong(value.toString());
                        } else {
                            args[0] = value;
                        }
                        descriptor.getWriteMethod().invoke(t, args);
                    }
                }
                b = true;
            } catch (IntrospectionException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return b;
        }
}
